import 'dart:async';
import 'dart:math';

import 'package:flutter/material.dart';
import 'package:flutter/material.dart';

import 'package:view_lamp/custom/view_lamp_item.dart';

import 'lamp_widget.dart';

///星星闪烁
///黄云音
///2020年2月25日

class CustomViewLamp4 extends StatefulWidget {
  final double multiColorWidth;
  final double multiColorHeight;
  final Color color;

  const CustomViewLamp4({
    Key key,
    this.multiColorWidth,
    this.multiColorHeight, this.color,
  }) : super(key: key);

  @override
  _CustomViewLamp4State createState() => _CustomViewLamp4State();
}

class _CustomViewLamp4State extends State<CustomViewLamp4> {
  Timer timer;
  List index = [0,1];
  // List<Color> list =[];
  List<ViewLampItem> lampItemInfoList = [
    ViewLampItem(17, 72, Colors.transparent, 'assets/images/previewbulb1-x17y72.png'),
    ViewLampItem(82, 90, Colors.transparent, 'assets/images/previewbulb2-x82y90.png'),
    ViewLampItem(147, 104, Colors.transparent, 'assets/images/previewbulb3-x147y104.png'),
    ViewLampItem(213, 106, Colors.transparent, 'assets/images/previewbulb4-x213y106.png'),
    ViewLampItem(279, 101, Colors.transparent, 'assets/images/previewbulb5-x279y101.png'),
    ViewLampItem(344, 90, Colors.transparent, 'assets/images/previewbulb6-x344y90.png'),
    ViewLampItem(409, 73, Colors.transparent, 'assets/images/previewbulb7-x409y73.png'),
    ViewLampItem(474, 60, Colors.transparent, 'assets/images/previewbulb8-x474y60.png'),
    ViewLampItem(540, 60, Colors.transparent, 'assets/images/previewbulb9-x540y60.png'),
    ViewLampItem(606, 70, Colors.transparent, 'assets/images/previewbulb10-x606y70.png'),
    ViewLampItem(670, 87, Colors.transparent, 'assets/images/previewbulb11-x670y87.png'),
    ViewLampItem(736, 102, Colors.transparent, 'assets/images/previewbulb12-x736y102.png'),
  ];

  @override
  void initState() {
    super.initState();
    timer = Timer.periodic(Duration(seconds: 1), (Timer t) => colorValue());
  }

  void colorValue() {
    setState(() {
      lampItemInfoList.forEach((element) {
        if(index[Random().nextInt(2)]==0){
          element.color = widget.color;
        }else if(index[Random().nextInt(2)]==1){
          element.color = Colors.transparent;
        }
      });
    });
  }

  @override
  Widget build(BuildContext context) {
    return LightChainWidget(lampItemInfoList: lampItemInfoList, multiColorWidth: widget.multiColorWidth,multiColorHeight:widget.multiColorHeight);
  }

  @override
  void dispose() {
    timer?.cancel();
    super.dispose();
  }
}
